//
//  BasicParameterVC.m
//  RTUApp
//
//  Created by 陈德军 on 2017/5/18.
//  Copyright © 2017年 陈德军. All rights reserved.
//

#import "BasicParameterVC.h"
#import "SearchEquipmentVC.h"
#import "OperationalLimitVC.h"
#import "EssentialParameterVC.h"
#import "SongtasteVC.h"
#import "GroundwaterVC.h"
#import "AcquisitionParametersVC.h"
#import "FeelingCurveVC.h"
#import "CommunicationVC.h"
#import "EquipmentParametersVC.h"
#import "KernelParameterVC.h"



@interface BasicParameterVC ()<SearchEquipmentVCDeleagte,CBPeripheralDelegate>{

}
/**
 *  顶部图片
 */
@property (nonatomic,strong) UIImageView *topImageView;
/**
 *
 */
@property (nonatomic,strong) MyLinearLayout *contentLayout;

/**
 *  蓝牙中心管理者
 */
@end

@implementation BasicParameterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"蓝牙连接断开";
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.backgroundColor = [UIColor whiteColor];
    self.view = scrollView;
    [kHMBlueToothManager beginScanCBPeripheral:^(NSArray *peripheralArr) {
        
    }];
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    contentLayout.myLeft = 0;
    contentLayout.myRight = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize,10,1);
    [scrollView addSubview:contentLayout];
    self.contentLayout = contentLayout;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sousuo3"] style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBarItemEvent)];
    [[NSUserDefaults standardUserDefaults] setObject:@"断开连接" forKey:@"ConnectionStatus"];
    [self addTopImageView:contentLayout];
    [self addTheMenuView:contentLayout];
    
    kHMBlueToothManager.bluetoothConnectionDisconnect = ^(NSString *value) {
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
        [PopupView showMessageStr:@"蓝牙连接断开"];
    };
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ConnectionStatus"] isEqualToString:@"连接成功"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.48 green:0.69 blue:0.29 alpha:1.00];
        self.navigationItem.title = @"蓝牙连接成功";
    }else{
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)addTopImageView:(MyLinearLayout *)contentLayout{
    self.topImageView.top = 0;
    self.topImageView.left = 0;
    self.topImageView.size = CGSizeMake(kScreenWidth, kScreenHeight/4);
    self.topImageView.image = [UIImage imageNamed:@"banner"];
    [contentLayout addSubview:self.topImageView];
}

- (void)addTheMenuView:(MyLinearLayout *)contentLayout{
    //添加操作按钮。
    MyFlowLayout *actionLayout = [MyFlowLayout flowLayoutWithOrientation:MyLayoutViewOrientation_Vert arrangedCount:3];
    actionLayout.frame = CGRectMake(0, 0, kScreenWidth, 100*3);
    actionLayout.gravity = MyGravity_Horz_Fill;  //所有子视图水平填充，也就是所有子视图的宽度相等。
    actionLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5);
    actionLayout.subviewHSpace = 5;
    actionLayout.subviewVSpace = 5;
    [contentLayout addSubview:actionLayout];
    
    [actionLayout addSubview:[self createActionButton:@"基本参数" action:@selector(clickMenuEvent:) tag:10000 imageName:@"picture1"]];
    [actionLayout addSubview:[self createActionButton:@"运行参数" action:@selector(clickMenuEvent:) tag:20000 imageName:@"picture2"]];
    [actionLayout addSubview:[self createActionButton:@"采集参数" action:@selector(clickMenuEvent:) tag:30000 imageName:@"picture3"]];
    [actionLayout addSubview:[self createActionButton:@"情曲线参数" action:@selector(clickMenuEvent:) tag:40000 imageName:@"picture4"]];
    [actionLayout addSubview:[self createActionButton:@"通讯参数" action:@selector(clickMenuEvent:) tag:50000 imageName:@"picture5"]];
    [actionLayout addSubview:[self createActionButton:@"地下水参数" action:@selector(clickMenuEvent:) tag:60000 imageName:@"picture6"]];
    [actionLayout addSubview:[self createActionButton:@"设备参数" action:@selector(clickMenuEvent:) tag:70000 imageName:@"picture7"]];
    [actionLayout addSubview:[self createActionButton:@"终端版本读取" action:@selector(clickMenuEvent:) tag:80000 imageName:@"picture8"]];
    [actionLayout addSubview:[self createActionButton:@"核心参数" action:@selector(clickMenuEvent:) tag:90000 imageName:@"picture9"]];


}


//创建动作操作按钮。
-(UIView*)createActionButton:(NSString*)title action:(SEL)action tag:(NSInteger)tag imageName:(NSString*)imageName
{
    UIView *view = [UIView new];
    view.myHeight = 100;
    view.layer.borderColor = [UIColor lightGrayColor].CGColor;
    view.layer.borderWidth = 0.5;
    view.userInteractionEnabled = YES;
    view.tag = tag;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMenuEvent:)];
    [view addGestureRecognizer:gesture];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:imageName];
    imageView.size = CGSizeMake(58, 61);
    imageView.left = ((kScreenWidth-20)/3)/2-29;
    imageView.top = 8;
    [view addSubview:imageView];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [titleLabel sizeToFit];
    titleLabel.top = imageView.bottom+10;
    titleLabel.centerX = imageView.centerX;
    [view addSubview:titleLabel];
    
    return view;
}

#pragma mark - SearchEquipmentVCDeleagte 代理分组
-(void)delegateReverseTheValue:(CBPeripheral *)periphearl{
    self.peripheralC = periphearl;
    //  设置外设的代理
//    [self.peripheralC setDelegate:self];
//    [self.peripheralC discoverServices:nil];
}



#pragma mark 触发事件
- (void)clickMenuEvent:(UITapGestureRecognizer *)but{
    SearchEquipmentVC *searchVC = [SearchEquipmentVC new];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ConnectionStatus"] isEqualToString:@"连接成功"]) {
        if (but.view.tag == 10000) {
            EssentialParameterVC *vc = [EssentialParameterVC new];
            vc.title = @"基本参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 20000){
            OperationalLimitVC *vc = [OperationalLimitVC new];
            vc.title = @"运行参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 30000){
            AcquisitionParametersVC *vc = [AcquisitionParametersVC new];
            vc.title = @"采集参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 40000){
            FeelingCurveVC *vc = [FeelingCurveVC new];
            vc.title = @"情曲线参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 50000){
            CommunicationVC *vc = [CommunicationVC new];
            vc.title = @"通讯参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 60000){
            GroundwaterVC *vc = [GroundwaterVC new];
            vc.title = @"地下水参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 70000){
            EquipmentParametersVC *vc = [EquipmentParametersVC new];
            vc.title = @"设备参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 80000){
            SongtasteVC *vc = [SongtasteVC new];
            vc.title = @"终端版本读取";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (but.view.tag == 90000){
            KernelParameterVC *vc = [KernelParameterVC new];
            vc.title = @"核心参数";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            return;
        }
    }else{
        [self.navigationController pushViewController:searchVC animated:YES];
        [PopupView showMessageStr:@"请先连接蓝牙设备!"];
    }
    
}

- (void)clickRightBarItemEvent{
    SearchEquipmentVC *VC = [SearchEquipmentVC new];
    VC.delegate = self;
    [self.navigationController pushViewController:VC animated:YES];
}


#pragma mark - 懒加载
- (UIImageView *)topImageView{
    if (_topImageView==nil) {
        _topImageView = [[UIImageView alloc] init];
    }
    return _topImageView;
}





@end
